package labs.laba_03;

import java.util.Calendar;
import java.util.Timer;

import labs.laba_03.FigureTypes;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View.OnTouchListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	static enum ModeVariants{
		add,
		move
	}

	private RunManager mRunManager;
	private OnClickListener buttonClickListener = new OnClickListener() {			
		@Override
		public void onClick(View v) {
			if(v.getId()==10){
				mRunManager.setType(FigureTypes.Circle);
			}else if(v.getId()==11){
				mRunManager.setType(FigureTypes.Rectangle);
			}if(v.getId()==12){
				mRunManager.setType(FigureTypes.Triangle);
			}if(v.getId()==13){
				if(mRunManager.getMode()==ModeVariants.add)
					mRunManager.setMode(ModeVariants.move);
				else
					mRunManager.setMode(ModeVariants.add);
				Button button = (Button)v;
				button.setText(String.valueOf(mRunManager.getMode()));
			}
			mSetTitle();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
	}
	@Override
	protected void onResume() {
		super.onResume();

		setContentView(R.layout.activity_main);
		SurfaceView mSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
		mSurfaceView.setClickable(true);

		if(mRunManager==null){
			mRunManager = new RunManager(mSurfaceView, mSurfaceView.getContext());
			//SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
			//mRunManager.setSurfaceHolder(mSurfaceHolder);
		}else{
			FCanvas q = mRunManager.getRoot();
			mRunManager = new RunManager(mSurfaceView, mSurfaceView.getContext());
			mRunManager.setRoot(q);
			q = null;
		}

		mSetTitle();

		LinearLayout mll = (LinearLayout)findViewById(R.id.linearLayout1);
		Button nb;
		short i = 0;
		for(FigureTypes ft: FigureTypes.values()){
			if(ft==FigureTypes.Canvas) continue;
			nb = new Button(mll.getContext());
			nb.setText(ft.toString().subSequence(0, ft.toString().length()));
			nb.setId(10+i++);
			nb.setOnClickListener(buttonClickListener);
			mll.addView(nb);
		}
		nb = new Button(mll.getContext());
		nb.setText("add");
		nb.setId(10+i++);
		nb.setOnClickListener(buttonClickListener);
		mll.addView(nb);
	}
	@Override
	protected void onPause() {
		mRunManager.stop();
		super.onPause();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		menu.add(0, 1, 0, "Clear").setIcon(android.R.drawable.ic_menu_rotate);
		menu.add(0, 2, 0, "Exit").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getTitle()=="Exit"){
			this.finish();
		}else if(item.getTitle()=="Clear"){
			mRunManager.resetRoot();
		}
		return super.onOptionsItemSelected(item);
	}
	private void mSetTitle(){
		setTitle(getString(R.string.app_name) + " : [" + String.valueOf(mRunManager.getMode())+ "] : [" + String.valueOf(mRunManager.getType()) +"]");
	}
}
